package model

import (
	"github.com/lib/pq"
	"manager/model/entity"
	"time"
)

// FirmwaresTacticCreateRequest 创建现场数据
type FirmwaresTacticCreateRequest struct {
	FirmwareId int64 `json:"firmware_id"`

	ReleaseTime int64 `json:"release_time"`

	ReleaseType int `json:"release_type"`

	UpgradeType int `json:"upgrade_type"`

	ExecRange int `json:"exec_range"`

	Status int `json:"status"`

	Desc string `json:"desc"`

	Count int `json:"count"`

	UpdatedAt int64 `json:"updated_at"`

	EbikesType pq.Int64Array `json:"ebikes_type"`

	SnCode []string `json:"sn_code"`
}

// FirmwaresTacticUpdateRequest 更新现场数据
type FirmwaresTacticUpdateRequest struct {
	Id int64 `json:"id"`

	FirmwareId *int64 `json:"firmware_id"`

	ReleaseTime *int64 `json:"release_time"`

	ReleaseType *int `json:"release_type"`

	UpgradeType *int `json:"upgrade_type"`

	ExecRange *int `json:"exec_range"`

	Status *int `json:"status"`

	Desc *string `json:"desc"`

	CreatedAt int64 `json:"created_at"`

	UpdatedAt *int64 `json:"updated_at"`

	EbikesType pq.Int64Array `json:"ebikes_type"`

	SnCode []string `json:"sn_code"`
}

// FirmwaresTacticListRequest 列表现场数据
type FirmwaresTacticListRequest struct {
	Index int `json:"index"`
	Size  int `json:"size"`

	Id int64 `json:"id"`

	FirmwareId *int64 `json:"firmware_id"`

	ReleaseTime *int64 `json:"release_time"`

	ReleaseType *int `json:"release_type"`

	UpgradeType *int `json:"upgrade_type"`

	ExecRange *int `json:"exec_range"`

	Status *int `json:"status"`

	Desc *string `json:"desc"`

	UpdatedAt *int64 `json:"updated_at"`

	TimeZone []int64 `json:"time_zone"`
}

// FirmwaresTacticListResponse 列表回包数据
type FirmwaresTacticListResponse struct {
	Total int                    `json:"total"`
	List  []*FirmwaresTacticInfo `json:"list"`
}

// FirmwaresTacticInfoRequest 列表现场数据
type FirmwaresTacticInfoRequest struct {
	Id int64 `json:"id"`

	FirmwareId *int64 `json:"firmware_id"`

	ReleaseTime *int64 `json:"release_time"`

	ReleaseType *int `json:"release_type"`

	UpgradeType *int `json:"upgrade_type"`

	ExecRange *int `json:"exec_range"`

	Status *int `json:"status"`

	Desc *string `json:"desc"`

	UpdatedAt *int64 `json:"updated_at"`
}

// FirmwaresTacticInfo 详细数据
type FirmwaresTacticInfo struct {
	Id int64 `json:"id"`

	FirmwareId int64 `json:"firmware_id"`

	ReleaseTime int64 `json:"release_time"`

	ReleaseType int `json:"release_type"`

	UpgradeType int `json:"upgrade_type"`

	ExecRange int `json:"exec_range"`

	Status int `json:"status"`

	Desc string `json:"desc"`

	Progress string `json:"progress"`

	Count int `json:"count"`

	CreatedAt int64 `json:"created_at"`

	UpdatedAt int64 `json:"updated_at"`
}

type UpgradePlanBySnResponse struct {
	FirmwareId int64 `json:"firmware_id"`

	TacticsId int64 `json:"tactics_id"`

	Version string `json:"version"`

	FirmwaresName string `json:"firmwares_name"`

	Path string `json:"path"`

	ReleaseType int `json:"release_type"`
}

// FirmwaresTacticDeleteRequest 删除现场数据
type FirmwaresTacticDeleteRequest struct {
	Id int64 `json:"id"`
}

// FirmwaresTacticTerminateRequest 终止计划
type FirmwaresTacticTerminateRequest struct {
	Id int64 `json:"id"`
}

type FirmwaresFindByBikeRequest struct {
	Id int64 `json:"id"`
}
type FirmwaresFindByBikeResponse struct {
	SnCode *[]string `json:"sn_code"`
}

type FirmwaresFindByTypeRequest struct {
	Id int64 `json:"id"`
}
type FirmwaresFindByTypeResponse struct {
	EbikesType pq.Int64Array `json:"ebikes_type"`
}

// FirmwaresTacticsEntityToDto entity数据转换
func FirmwaresTacticsEntityToDto(firmwaresTactics []*entity.FirmwaresTactic) []*FirmwaresTacticInfo {
	out := make([]*FirmwaresTacticInfo, 0, len(firmwaresTactics))
	for _, c := range firmwaresTactics {
		if c.Status != 2 && c.UpgradeType == 1 && c.ReleaseTime >= time.Now().Unix() {
			c.Status = 0
		}
		out = append(out, FirmwaresTacticEntityToDto(c))
	}
	return out
}

// FirmwaresTacticEntityToDto entity数据转换
func FirmwaresTacticEntityToDto(e *entity.FirmwaresTactic) *FirmwaresTacticInfo {
	return &FirmwaresTacticInfo{

		Id: e.Id,

		FirmwareId: e.FirmwareId,

		ReleaseTime: e.ReleaseTime,

		ReleaseType: e.ReleaseType,

		UpgradeType: e.UpgradeType,

		ExecRange: e.ExecRange,

		Status: e.Status,

		Desc: e.Desc,

		Count: e.Count,

		CreatedAt: e.CreatedAt,

		UpdatedAt: e.UpdatedAt,
	}
}
